import java.util.Arrays;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{ 1,2,3,4,5,6,7,8,9 };
        bubblesort(arr);
        System.out.println(myToString(arr));


        if(find(arr,0) != -1){
            System.out.println("找到了下标为" + find(arr,0));
        }else {
            System.out.println("找不到");
        }
    }


    public static String myToString(int[] arr){
        String tmp = "[";
        for (int i = 0; i < arr.length; i++) {
            tmp += arr[i];
            if(i != arr.length - 1){
                tmp += ',';
            }
        }
        tmp += ']';
        return tmp;
    }
    public static void bubblesort(int[] arr){
        int len = arr.length;
        for(int i =  0; i < len - 1; i++){
            boolean flag = true;
            for (int j = 0; j < len - i - 1; j++) {
                if(arr[j] > arr[j + 1]){
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flag = false;
                }
            }
            if(flag) break;
        }
    }


    public static int find(int[] arr,int x){
        Arrays.sort(arr);
        int right = arr.length - 1;
        int left = 0;

        while(left <= right){
            int mid = (right + left) / 2;
            if(arr[mid] == x){
                return mid;
            } else if (arr[mid] < x) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }
}